package com.xcc.mota;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.xcc.mota.event.TouchEvent;
import com.xcc.mota.screen.AboutScreen;
import com.xcc.mota.screen.BaseScreen;
import com.xcc.mota.screen.Level1Screen;
import com.xcc.mota.screen.MenuScreen;
import com.xcc.mota.utils.Logger;
import com.xcc.mota.utils.ScreenUtils;

import static com.xcc.mota.screen.BaseScreen.ScreenEvent.MsgAbout;
import static com.xcc.mota.screen.BaseScreen.ScreenEvent.MsgLevel1;
import static com.xcc.mota.screen.BaseScreen.ScreenEvent.MsgMenu;
import static com.xcc.mota.screen.BaseScreen.ScreenEvent.TypeExitGame;
import static com.xcc.mota.screen.BaseScreen.ScreenEvent.TypeOpenScreen;

/**
 * Created by Administrator on 2018/1/8.
 */
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable, BaseScreen.OnScreenEvent {
    public GameView(Context context) {
        super(context);
        init(context);
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public GameView setWidthHeight(int[] widthHeight) {
        if (widthHeight[0] > 0 && widthHeight[1] > 1) this.widthHeight = widthHeight;
        return this;
    }

    public GameView setRuning(boolean runing) {
        isRuning = runing;
        return this;
    }

    private void init(Context context) {
        this.context = context;
        int[] screenWidthHeight = ScreenUtils.getScreenWidthHeight(context);
        setWidthHeight(screenWidthHeight);
        setRuning(true);

        holder = getHolder();
        holder.addCallback(this);
    }

    //游戏场景尺寸720x1280
    //public static final int ScreenWidth = 720;//544
    public static final int ScreenWidth = 544;
    //public static final int ScreenHeight = 1280;//992
    public static final int ScreenHeight = 992;
    private int[] widthHeight;
    private SurfaceHolder holder;
    private Context context;
    private boolean isShowing;
    private boolean isRuning;
    private Thread thread;
    private BaseScreen baseScreen;
    private float scale, translate;

    public void surfaceCreated(SurfaceHolder holder) {
        isShowing = true;
//        if (thread != null && thread.isAlive()) {
//            thread.stop();
//        }
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
        Logger.out("创建");
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        int[] screenWidthHeight = ScreenUtils.getScreenWidthHeight(context);
        setWidthHeight(screenWidthHeight);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        isShowing = false;
        Logger.out("销毁");
    }

    public void run() {
        while (isRuning) {
            while (isShowing) {
                if (holder != null) {
                    Canvas canvas = holder.lockCanvas();
                    if (canvas != null) {
                        long time;
                        if (Logger.isDebug) {
                            time = System.currentTimeMillis();
                        }

                        //抗锯齿
                        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                        //扯画布
                        float wf = widthHeight[0] * 1.0f / ScreenWidth;
                        float hf = widthHeight[1] * 1.0f / ScreenHeight;
                        if (wf > hf) {
                            scale = hf;
                            int i = widthHeight[0] - ScreenWidth;
                            translate = i / 2;
                            canvas.translate(translate, 0);
                            canvas.scale(scale, scale, ScreenWidth / 2, 0);
                        } else {
                            translate = 0;
                            scale = wf;
                            canvas.scale(scale, scale, 0, 0);
                        }

                        drawScreen(canvas);

                        if (Logger.isDebug) {
                            time = System.currentTimeMillis() - time;
                            Paint paint = new Paint();
                            paint.setTextSize(25);
                            paint.setColor(0xFFff0000);
                            canvas.drawText("" + time, 10, 20, paint);
                        }

                        //还原画布
                        //canvas.scale(2 - wf, 2 - wf, 0, 0);
                        holder.unlockCanvasAndPost(canvas);
                    }
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void drawScreen(Canvas canvas) {
        if (baseScreen == null) {
            baseScreen = new MenuScreen(context).setOnScreenEvent(this);
        }
        baseScreen.onDraw(canvas);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (baseScreen == null) {
            return super.onTouchEvent(event);
        }
        if (Logger.isDebug) {
            Logger.out("x:" + event.getX());
            Logger.out("y:" + event.getY());
        }
        TouchEvent touchEvent = new TouchEvent(event.getAction());
        //偏移计算
        touchEvent.setY(event.getY() / scale);
        if (translate == 0) {
            touchEvent.setX(event.getX() / scale);
        } else {
            float x = ScreenWidth / 2 + (event.getX() - widthHeight[0] / 2) / scale;
            touchEvent.setX(x);
        }
        baseScreen.onTouchEvent(touchEvent);
        return true;
    }

    public void onEvent(int type, int msg) {
        switch (type) {
            case TypeExitGame:
                if (context != null && context instanceof Activity) {
                    Activity activity = (Activity) context;
                    activity.onBackPressed();
                }
                break;
            default:
            case TypeOpenScreen:
                switch (msg) {
                    default:
                    case MsgMenu:
                        baseScreen = new MenuScreen(context);
                        break;
                    case MsgAbout:
                        baseScreen = new AboutScreen(context);
                        break;
                    case MsgLevel1:
                        baseScreen = new Level1Screen(context);
                        break;
                }
                baseScreen.setOnScreenEvent(this);
                break;
        }
    }
}
